package testhupai;

import java.util.Arrays;


/**
 *
 * @author zkpursuit
 */
public final class AI {

    /**
     * 递归方式判断平胡
     *
     * @param cardClusterArray 牌号和牌数量的簇集对象集合
     * @param len 所有牌数量
     * @return true表示可以胡牌
     */
    private static boolean isPingHu(int[] cardClusterArray, int startIndex, int len) {
        if (len == 0) {
            return true;
        }
        int i;
        if (len % 3 == 2) {
            //移除一对（两张牌），胡牌中必须包含一对
            for (i = startIndex; i < cardClusterArray.length; i++) {
                if (cardClusterArray[i] >= 2) {
                    cardClusterArray[i] -= 2;
                    if (AI.isPingHu(cardClusterArray, startIndex, len - 2)) {
                        return true;
                    }
                    cardClusterArray[i] += 2;
                }
            }
        } else {
            //是否是顺子
            int loopCount = cardClusterArray.length - 2;
            for (i = startIndex; i < loopCount; i++) {
                int idx1 = i + 1;
                int idx2 = i + 2;
                int type1 = HandCards.getCardLeftValueByClusterIndex(i);
                int type2 = HandCards.getCardLeftValueByClusterIndex(idx1);
                int type3 = HandCards.getCardLeftValueByClusterIndex(idx2);
                if (cardClusterArray[i] > 0 && cardClusterArray[idx1] > 0 && cardClusterArray[idx2] > 0 && type1 < 4 && type2 < 4 && type3 < 4) {
                    cardClusterArray[i] -= 1;
                    cardClusterArray[idx1] -= 1;
                    cardClusterArray[idx2] -= 1;
                    if (AI.isPingHu(cardClusterArray, startIndex, len - 3)) {
                        return true;
                    }
                    cardClusterArray[i] += 1;
                    cardClusterArray[idx1] += 1;
                    cardClusterArray[idx2] += 1;
                }
            }
            //三个一样的牌（暗刻）
            for (i = startIndex; i < cardClusterArray.length; i++) {
                if (cardClusterArray[i] >= 3) {
                    cardClusterArray[i] -= 3;
                    if (AI.isPingHu(cardClusterArray, startIndex, len - 3)) {
                        return true;
                    }
                    cardClusterArray[i] += 3;
                }
            }
        }
        return false;
    }

    /**
     * 递归方式判断平胡
     *
     * @param mycards 手牌
     * @return true表示可以胡牌
     */
    public static boolean isPingHu(HandCards mycards) {
        int[] cardClusterArray = mycards.getCardClusterArray();
        int totals = mycards.getCardTotals();
        if (totals % 3 != 2) {
            return false;
        }
        return AI.isPingHu(cardClusterArray, mycards.getClusterValidStartIndex(), totals);
    }

    public static void main(String[] args) {
        HandCards handCards = new HandCards(new int[]{11, 12, 11, 22, 22, 24, 31, 32, 33, 36, 36});
        System.out.println(Arrays.toString(handCards.getCardClusterArray()));
        System.out.println(Arrays.toString(handCards.getCards()));
        long time1=System.currentTimeMillis();	
        for (int i = handCards.getClusterValidStartIndex(); i <= handCards.getClusterValidEndIndex(); i++) {
            int card = handCards.getCardByClusterIndex(i);
            if (card > 0) {
                int num = handCards.getCardNum(card);
                System.out.println(num + "张  " + CardType.getCardName(card));
            }
        }
        boolean bool = isPingHu(handCards);
        long time2=System.currentTimeMillis();
        System.out.println("是否胡牌：" + bool);
        System.out.println("当前程序耗时："+(time2-time1)+"ms");
    }

}